/**
 * @author Eduardo Leonne Souza de Oliveira
 * @version 1.0
 * Date: 2011-12-29
 * Last change: 2011-12-29 
 * 
 */

package com.leonne.harpacristabrasileira.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;

import com.leonne.harpacristabrasileira.R;
import com.leonne.harpacristabrasileira.dao.AnthemsDAO;
import com.leonne.harpacristabrasileira.dao.ConfigurationsDAO;
import com.leonne.harpacristabrasileira.tools.CustomWaitDialog;
import com.leonne.harpacristabrasileira.tools.DataCache;
import com.leonne.harpacristabrasileira.tools.MyConstants;
import com.leonne.harpacristabrasileira.tools.Utils;

public class MainActivity extends Activity implements
OnGestureListener {
	
	private GestureDetector gestureScanner;
	
    /**
     * (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Utils.isWideScreen(this.getBaseContext()))
        	setContentView(R.layout.main_wide);
        else
        	setContentView(R.layout.main);
        
        this.gestureScanner = new GestureDetector(this);
        
        // Initialize the database
        CustomWaitDialog cwd = new CustomWaitDialog(MainActivity.this, "initializeData", "startApp", false);
		cwd.execute();
    }
	
	public void initializeData(){
		ConfigurationsDAO cdao = new ConfigurationsDAO(getBaseContext());
        int value = Integer.parseInt(cdao.fetchByDescription("FONTSIZE"));
        MyConstants.SELECTEDFONTSIZE = value;
		AnthemsDAO dao = new AnthemsDAO(this.getBaseContext());
        DataCache.anthemsCache = dao.fetch("");
	}
	
	public void startApp(){
		ConfigurationsDAO cdao = new ConfigurationsDAO(getBaseContext());
		int firstTime = Integer.parseInt(cdao.fetchByDescription("FIRSTTIME"));
		Intent intent;
		if (firstTime == 1){
			intent = new Intent(MainActivity.this, HelpActivity.class);
			intent.putExtra("IDANTHEM", -1);
		} else {
			intent = new Intent(MainActivity.this, FirstPageActivity.class);
			intent.putExtra("FORCESEARCH", false);
			MyConstants.ISFAVORITE = false;
		}
		startActivity(intent);
		overridePendingTransition(R.anim.fade, R.anim.hold);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent me) {
		return gestureScanner.onTouchEvent(me);
	}

	public boolean onDown(MotionEvent e) {
		if (DataCache.anthemsCache != null)
			startApp();
		return false;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		return false;
	}

	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
	
}